﻿using System;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic.ClassFactory;
using Common;
using DataAccess;
using Util.PageInfo;
using ValidateCommon;

namespace RichECMS.ServerPages.Comment
{
    public partial class List : Util.BasePage.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                State.strWhereSql = " 1=1 ";
                State.intPageIndex = 1;
                ShowCommentData(State.intPageIndex, AspNetPagerList.PageSize);
            }
        }

        private void ShowCommentData(int intCurrentPage, int intPageSize)
        {
            if (Request["Aid"] != null || Request["Sid"] != null)
            {
                if (ParamAuth.IsNumber(Request["Aid"].ToString()))
                {
                    int intArticleID = int.Parse(Request["Aid"].ToString());
                    InitDataBindByArticle(intCurrentPage, intPageSize, intArticleID);
                    AspNetPagerList.CurrentPageIndex = intCurrentPage;
                    LitPaagePath.Text = "文章评论管理";
                    LitPageName.Text = "文章评论管理";
                }
                else if (ParamAuth.IsNumber(Request["Sid"].ToString()))
                {
                    int intSectionID = int.Parse(Request["Sid"].ToString());
                    InitDataBindBySection(intCurrentPage, intPageSize, intSectionID);
                    AspNetPagerList.CurrentPageIndex = intCurrentPage;
                    LitPaagePath.Text = "栏目评论管理";
                    LitPageName.Text = "栏目评论管理";
                }
                else
                {
                    Alert("查询参数不正确");
                    return;
                }
            }
            else
            {
                InitDataBindAll(intCurrentPage, intPageSize);
                AspNetPagerList.CurrentPageIndex = intCurrentPage;
                LitPaagePath.Text = "评论管理";
                LitPageName.Text = "评论管理";
            }
        }

        /// <summary>
        /// 绑定
        /// </summary>
        private void InitDataBindAll(int intCurrentPage, int intPageSize)
        {
            try
            {
                using (CommentFactory objFactory = new CommentFactory())
                {
                    StringBuilder strWhere = new StringBuilder();
                    int intPageFrom = intCurrentPage * intPageSize;
                    int intTotalCounts = 0;
                    BusinessLogic.BusinessRules.Comment objComment = objFactory.NewInstance();

                    intTotalCounts = objComment.GetTotalCountByCustomSql("SELECT count(*) FROM (select Top " + (intPageFrom).ToString() + " * from Comment where " + State.strWhereSql + " order by intSort desc ,dtmInTime desc,intCommentID desc)t ");
                    LitPageCurrent.Text = intCurrentPage.ToString();
                    LitPageCount.Text = intTotalCounts.ToString();
                    if (intTotalCounts % intPageSize != 0)
                    {
                        if ((intCurrentPage == intTotalCounts / intPageSize + 1))
                        {
                            intPageFrom = (intCurrentPage - 1) * intPageSize;
                            //intPageSize =Model.Config.JudgeDatabase( intTotalCounts % intPageSize);//处理不同数据库差异
                            intPageSize = intTotalCounts % intPageSize;
                            intPageFrom = intPageFrom + intPageSize;
                        }
                    }
                    strWhere.Append("select * from (select top " + intPageSize + " * from  (select Top " + (intPageFrom).ToString() + " * from (select Top " + (intPageFrom).ToString() + " * from Comment where " + State.strWhereSql + " order by intSort desc ,dtmInTime desc,intCommentID desc)t order by intSort asc, dtmInTime asc ,intCommentID asc)m " + " order by intSort asc, dtmInTime asc ,intCommentID asc)mm  order by intSort desc, dtmInTime desc ,intCommentID desc");
                    objComment = objFactory.CreatorByFullSql(strWhere.ToString());
                    if (objComment != null)
                    {
                        rptList.DataSource = objComment.Data.Comment.DataSet.Tables[0].DefaultView;
                        AspNetPagerList.RecordCount = intTotalCounts;
                        rptList.DataBind();
                    }
                    else
                    {
                        rptList.DataSource = objFactory.NewInstance().Data.Comment.DataSet.Tables[0].DefaultView;
                        AspNetPagerList.RecordCount = 0;
                        rptList.DataBind();
                        //Alert("暂无数据！");
                    }
                }
            }
            catch (Exception ex)
            {
                Alert(ex.ToString().Replace("\r\n", " "));
            }
        }

        private void InitDataBindByArticle(int intCurrentPage, int intPageSize, int intArticleId)
        {
            try
            {
                using (CommentFactory objFactory = new CommentFactory())
                {
                    StringBuilder strWhere = new StringBuilder();
                    int intPageFrom = intCurrentPage * intPageSize;
                    int intTotalCounts = 0;
                    BusinessLogic.BusinessRules.Comment objComment = objFactory.NewInstance();

                    intTotalCounts = objComment.GetTotalCountByCustomSql("SELECT count(*) FROM (select Top " + (intPageFrom).ToString() + " * from Comment where intArticleId=" + intArticleId + State.strWhereSql + " order by intSort desc ,dtmInTime desc,intCommentID desc)t ");
                    LitPageCurrent.Text = intCurrentPage.ToString();
                    LitPageCount.Text = intTotalCounts.ToString();
                    if (intTotalCounts % intPageSize != 0)
                    {
                        if ((intCurrentPage == intTotalCounts / intPageSize + 1))
                        {
                            intPageFrom = (intCurrentPage - 1) * intPageSize;
                            //intPageSize =Model.Config.JudgeDatabase( intTotalCounts % intPageSize);//处理不同数据库差异
                            intPageSize = intTotalCounts % intPageSize;
                            intPageFrom = intPageFrom + intPageSize;
                        }
                    }
                    strWhere.Append("select * from (select top " + intPageSize + " * from  (select Top " + (intPageFrom).ToString() + " * from (select Top " + (intPageFrom).ToString() + " * from Comment where intArticleId=" + intArticleId + State.strWhereSql + " order by intSort desc ,dtmInTime desc,intCommentID desc)t order by intSort asc, dtmInTime asc ,intCommentID asc)m " + " order by intSort asc, dtmInTime asc ,intCommentID asc)mm  order by intSort desc, dtmInTime desc ,intCommentID desc");
                    objComment = objFactory.CreatorByFullSql(strWhere.ToString());
                    if (objComment != null)
                    {
                        rptList.DataSource = objComment.Data.Comment.DataSet.Tables[0].DefaultView;
                        AspNetPagerList.RecordCount = intTotalCounts;
                        rptList.DataBind();
                    }
                    else
                    {
                        rptList.DataSource = objFactory.NewInstance().Data.Comment.DataSet.Tables[0].DefaultView;
                        AspNetPagerList.RecordCount = 0;
                        rptList.DataBind();
                        //Alert("暂无数据！");
                    }
                }
            }
            catch (Exception ex)
            {
                Alert(ex.ToString().Replace("\r\n", " "));
            }
        }

        private void InitDataBindBySection(int intCurrentPage, int intPageSize, int intSectionID)
        {
            try
            {
                using (CommentFactory objFactory = new CommentFactory())
                {
                    StringBuilder strWhere = new StringBuilder();
                    int intPageFrom = intCurrentPage * intPageSize;
                    int intTotalCounts = 0;
                    BusinessLogic.BusinessRules.Comment objComment = objFactory.NewInstance();

                    intTotalCounts = objComment.GetTotalCountByCustomSql("SELECT count(*) FROM (select Top " + (intPageFrom).ToString() + " * from Comment where intSectionID=" + intSectionID + State.strWhereSql + " order by intSort desc ,dtmInTime desc,intCommentID desc)t ");
                    LitPageCurrent.Text = intCurrentPage.ToString();
                    LitPageCount.Text = intTotalCounts.ToString();
                    if (intTotalCounts % intPageSize != 0)
                    {
                        if ((intCurrentPage == intTotalCounts / intPageSize + 1))
                        {
                            intPageFrom = (intCurrentPage - 1) * intPageSize;
                            //intPageSize =Model.Config.JudgeDatabase( intTotalCounts % intPageSize);//处理不同数据库差异
                            intPageSize = intTotalCounts % intPageSize;
                            intPageFrom = intPageFrom + intPageSize;
                        }
                    }
                    strWhere.Append("select * from (select top " + intPageSize + " * from  (select Top " + (intPageFrom).ToString() + " * from (select Top " + (intPageFrom).ToString() + " * from Comment where intSectionID=" + intSectionID + State.strWhereSql + " order by intSort desc ,dtmInTime desc,intCommentID desc)t order by intSort asc, dtmInTime asc ,intCommentID asc)m " + " order by intSort asc, dtmInTime asc ,intCommentID asc)mm  order by intSort desc, dtmInTime desc ,intCommentID desc");
                    objComment = objFactory.CreatorByFullSql(strWhere.ToString());
                    if (objComment != null)
                    {
                        rptList.DataSource = objComment.Data.Comment.DataSet.Tables[0].DefaultView;
                        AspNetPagerList.RecordCount = intTotalCounts;
                        rptList.DataBind();
                    }
                    else
                    {
                        rptList.DataSource = objFactory.NewInstance().Data.Comment.DataSet.Tables[0].DefaultView;
                        AspNetPagerList.RecordCount = 0;
                        rptList.DataBind();
                        //Alert("暂无数据！");
                    }
                }
            }
            catch (Exception ex)
            {
                Alert(ex.ToString().Replace("\r\n", " "));
            }
        }

        /// <summary>
        /// 搜索
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ImageButtonsearch(object sender, ImageClickEventArgs e)
        {
            StringBuilder sbWhere = new StringBuilder();
            if (txtTitle.Value.Trim() != "")
            {
                sbWhere.AppendFormat(" and chvContent like '%{0}%'", txtTitle.Value.Trim());
            }
            if (txtDatetime.Value.Trim() != "")
            {
                sbWhere.AppendFormat(" and dtmInTime>{0}", txtDatetime.Value.Trim());
            }
            State.strWhereSql = sbWhere.ToString();
            State.intPageIndex = 1;
            ShowCommentData(State.intPageIndex, AspNetPagerList.PageSize);
        }

        /// <summary>
        /// 删除所选
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDeleteAll_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                using (DBManager objDBManager = new DBManager())
                {
                    objDBManager.BeginTransaction();
                    try
                    {
                        CommentFactory objFactory = new CommentFactory(objDBManager);
                        objFactory.DeleteCommentWhere(" intCommentID in (" + SortHidden.Value.Trim() + ")");

                        //提交
                        objDBManager.Commit();
                        objFactory.Dispose();
                        AlertAndRedirect("删除成功", "List.aspx", false);
                    }
                    catch (Exception ex)
                    {
                        Alert(ex.ToString().Replace("\r\n", " "));
                        objDBManager.Rollback();
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                Alert(ex.ToString().Replace("\r\n", " "));
            }
        }

        /// <summary>
        /// 批量放入回收站
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnRecovery_Click(object sender, ImageClickEventArgs e)
        {
        }

        /// <summary>
        /// 分页方法
        /// </summary>
        private void BindToPager()
        {
        }

        /// <summary>
        /// 分页  PageChanged
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AspNetPagerArticle_PageChanged(object sender, EventArgs e)
        {
            State.intPageIndex = AspNetPagerList.CurrentPageIndex;
            ShowCommentData(AspNetPagerList.CurrentPageIndex, AspNetPagerList.PageSize);
        }

        /// <summary>
        /// rptList_ItemCommand  编辑   放入回收站  删除
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        protected void rptList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                using (DBManager objDBManager = new DBManager())
                {
                    int intCommentID = Convert.ToInt32(e.CommandArgument);
                    objDBManager.BeginTransaction();
                    try
                    {
                        CommentFactory objFactory = new CommentFactory(objDBManager);
                        objFactory.DeleteComment(intCommentID);

                        //提交
                        objDBManager.Commit();
                        objFactory.Dispose();
                        AlertAndRedirect("删除成功", "List.aspx", false);
                    }
                    catch (Exception ex)
                    {
                        Alert(ex.ToString().Replace("\r\n", " "));
                        objDBManager.Rollback();
                        return;
                    }
                }
            }
            //放入回收站
            if (e.CommandName == "Recycle")
            {
            }
        }
    }
}